package jgl.vajra.kingkong.part.order.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.bean.AccountingMethodBean;
import jgl.vajra.kingkong.bean.OrderEvaluateBean;
import jgl.vajra.kingkong.databinding.ActivityOrderEvaluateBinding;
import jgl.vajra.kingkong.databinding.ItemAccountingMethodBinding;
import jgl.vajra.kingkong.databinding.ItemOrderEvaluateBinding;
import jgl.vajra.kingkong.manager.IntentManager;
import jgl.vajra.kingkong.part.order.contract.OrderEvaluateContract;
import jgl.vajra.kingkong.part.order.viewModel.OrderEvaluateViewModel;
import jgl.vajra.kingkong.utils.PictureUtils;
import jgl.vajra.kingkong.utils.RandomUtils;
import jgl.vajra.kingkong.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.kingkong.widget.databindingadapter.BaseDataBindingDecorator;
import jgl.vajra.kingkong.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.kingkong.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.kingkong.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.kingkong.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.kingkong.widget.utils.ToastUtils;

@CreateViewModel(OrderEvaluateViewModel.class)
public class OrderEvaluateActivity extends BaseMVVMActivity<OrderEvaluateViewModel, ActivityOrderEvaluateBinding> implements OrderEvaluateContract.View, BaseBindingItemPresenter<OrderEvaluateBean.OrderEvaluateTitleBean.TitleListBean> {

    private OrderEvaluateBean data;
    private SingleTypeBindingAdapter adapter;
    private String workCapacity;
    private String capacityDes;
    private String workSkill;
    private String skillDes;
    private String safetyStandard;
    private String safetyDes;
    private String serviceAttitude;
    private String attitudeDes;

    public List<Map<String, String>> picture = new ArrayList<>();
    private String orderId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_order_evaluate;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);

        orderId = getIntent().getStringExtra("orderId");

        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SingleTypeBindingAdapter<>(this, null, R.layout.item_order_evaluate);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemDecorator((BaseDataBindingDecorator<OrderEvaluateBean.OrderEvaluateTitleBean, ItemOrderEvaluateBinding>) (binding, position, viewType, data) -> {

            // 不可嵌套滑动
            binding.itemRecyclerView.setNestedScrollingEnabled(false);
            // 取消head 的 recycleView的焦点
            binding.itemRecyclerView.setFocusableInTouchMode(false);
            //设置布局管理器

            binding.itemRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
            SingleTypeBindingAdapter itemAdapter = new SingleTypeBindingAdapter(this, data.titleList, R.layout.item_order_evaluate_item);
            binding.itemRecyclerView.setAdapter(itemAdapter);
            itemAdapter.setItemPresenter(this);

        });

        requestNetData();

        mBinding.etContent.addTextChangedListener(new TextWatcher() {
            /**
             * 编辑框的内容发生改变之前的回调方法
             */
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            /**
             * 编辑框的内容正在发生改变时的回调方法 >>用户正在输入
             * 我们可以在这里实时地 通过搜索匹配用户的输入
             */
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            /**
             * 编辑框的内容改变以后,用户没有继续输入时 的回调方法
             */
            @Override
            public void afterTextChanged(Editable editable) {
                int length = editable.toString().length();
                mBinding.tvContentLength.setText("已写" + length + "个字/评论10个字以上(不超200字)再获得");
            }

        });
    }

    private void requestNetData() {
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AEACCoder.encrypt(RandomUtils.getRandom()));
        mViewModel.getOrderEvaluateData(map);
    }

    //带
    @Override
    public void onBelt() {
        data.isAccessories = true;
        mBinding.setData(data);
    }

    //不带
    @Override
    public void onNoBelt() {
        data.isAccessories = false;
        mBinding.setData(data);
    }

    //确定
    @Override
    public void onDetermine() {
        if (picture != null && picture.size() != 0) {
            picture.clear();
        }

        String comment = mBinding.etContent.getText().toString().trim();

        /*if (TextUtils.isEmpty(comment)) {
            ToastUtils.showShort("请输入评价");
            return;
        }
        if (comment.length() < 10) {
            ToastUtils.showShort("评价要10个字以上");
            return;
        }*/

        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        map.put("scoreLevel", mBinding.star.getStarStep());
        map.put("workCapacity", workCapacity);
        map.put("capacityDes", capacityDes);
        map.put("workSkill", workSkill);
        map.put("skillDes", skillDes);
        map.put("safetyStandard", safetyStandard);
        map.put("safetyDes", safetyDes);
        map.put("serviceAttitude", serviceAttitude);
        map.put("attitudeDes", attitudeDes);

        int again = 0;
        if (data.isAccessories) {
            again = 1;
        }
        map.put("again", again);

        map.put("comment", comment);

        List<String> pathList = mBinding.multiPicView.getPathList();

        Map<String, String> pictureMap = new HashMap<>();
        for (int i = 0; i < pathList.size(); i++) {
            String base64Picture = PictureUtils.bitmapToString(pathList.get(i));
            // picture.add(i + ".jpeg :" + base64Picture);

            Logger.d("图片"+base64Picture);

            pictureMap.put(i + ".jpeg", base64Picture);
        }
        if (pictureMap.size() != 0) {
            map.put("picture", pictureMap);
        }

        mViewModel.submitCommentsData(map);
    }

    @Override
    public void returnOrderEvaluateBean(OrderEvaluateBean data) {
        this.data = data;
        data.isAccessories = true;
        data.evaluateTitleList = new ArrayList<>();

        data.evaluateTitleList.add(convertData(data.workCapacity, "干活质量：", 0));
        data.evaluateTitleList.add(convertData(data.workSkill, "干活技术：", 1));
        data.evaluateTitleList.add(convertData(data.safetyStandard, "安全规范：", 2));
        data.evaluateTitleList.add(convertData(data.serviceAttitude, "服务态度：", 3));

        adapter.refresh(data.evaluateTitleList);
        mBinding.setData(data);

    }

    @Override
    public void returnSubmitCommentsData() {
        IntentManager.toCommentStateActivity(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mBinding.multiPicView.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onItemClick(int position, OrderEvaluateBean.OrderEvaluateTitleBean.TitleListBean itemData) {
        if (itemData.isClick) {
            return;
        }
        List<OrderEvaluateBean.OrderEvaluateTitleBean.TitleListBean> titleList = data.evaluateTitleList.get(itemData.position).titleList;
        for (int i = 0; i < titleList.size(); i++) {
            titleList.get(i).isClick = false;
        }
        titleList.get(position).isClick = true;
        adapter.refresh();

        switch (itemData.position) {
            case 0:
                workCapacity = titleList.get(position).key;
                capacityDes = titleList.get(position).value;
                break;
            case 1:
                workSkill = titleList.get(position).key;
                skillDes = titleList.get(position).value;
                break;
            case 2:
                safetyStandard = titleList.get(position).key;
                safetyDes = titleList.get(position).value;
                break;
            case 3:
                serviceAttitude = titleList.get(position).key;
                attitudeDes = titleList.get(position).value;
                break;
        }
    }

    //数据转换
    public OrderEvaluateBean.OrderEvaluateTitleBean convertData(List<Map<String, String>> data, String title, int position) {
        OrderEvaluateBean.OrderEvaluateTitleBean newWorkCapacity = new OrderEvaluateBean.OrderEvaluateTitleBean();
        newWorkCapacity.title = title;
        newWorkCapacity.titleList = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            OrderEvaluateBean.OrderEvaluateTitleBean.TitleListBean titleBean = new OrderEvaluateBean.OrderEvaluateTitleBean.TitleListBean();
            for (Map.Entry<String, String> entry : data.get(i).entrySet()) {
                titleBean.key = entry.getKey();
                titleBean.value = entry.getValue();
                titleBean.position = position;
                if (i == 0) {
                    titleBean.isClick = true;
                    switch (position) {
                        case 0:
                            workCapacity = titleBean.key;
                            capacityDes = titleBean.value;
                            break;
                        case 1:
                            workSkill = titleBean.key;
                            skillDes = titleBean.value;
                            break;
                        case 2:
                            safetyStandard = titleBean.key;
                            safetyDes = titleBean.value;
                            break;
                        case 3:
                            serviceAttitude = titleBean.key;
                            attitudeDes = titleBean.value;
                            break;
                    }
                }
            }
            newWorkCapacity.titleList.add(titleBean);
        }
        return newWorkCapacity;
    }
}
